package com.gollum.system.service.impl;

import com.gollum.common.context.PageContextHolder;
import com.gollum.common.domain.response.TableSupport;
import com.gollum.common.exception.BusinessException;
import com.gollum.common.utils.BeanUtils;
import com.gollum.common.utils.StringUtils;
import com.gollum.jpa.common.Specifications;
import com.gollum.jpa.domain.Page;
import com.gollum.jpa.service.JpaServerImpl;
import com.gollum.system.domain.model.SysDictData;
import com.gollum.system.domain.request.SysDictDataRequest;
import com.gollum.system.domain.response.SysDictDataResponse;
import com.gollum.system.domain.response.SysLogininforResponse;
import com.gollum.system.service.SysDictDataService;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 字典数据service
 *
 * @author Herther
 * @createTime 2022年09月18日 22:54:00
 * @since 1.0.0
 */
@Service
public class SysDictDataServiceImpl extends JpaServerImpl<SysDictData,Long> implements SysDictDataService {

    /**
     * 构建查询参数 init
     * @auther: Herther
     * @since 1.0.0
     * @date: 2022/9/18 23:22
     */
    private Specification<SysDictData> getParams(SysDictDataRequest sysDictDataRequest){
        Specification<SysDictData> spec = Specifications.<SysDictData>and()
                // 字典类型
                .eq(Objects.nonNull(sysDictDataRequest.getDictTypeId()),
                        SysDictData::getDictTypeId, sysDictDataRequest.getDictTypeId())
                // 字典标签
                .like(StringUtils.isNotEmpty(sysDictDataRequest.getDictLabel()),
                        SysDictData::getDictLabel, sysDictDataRequest.getDictLabel())
                // 字典键值
                .eq(StringUtils.isNotEmpty(sysDictDataRequest.getDictValue()),
                        SysDictData::getDictValue, sysDictDataRequest.getDictValue())
                // 是否默认（Y是 N否）
                .eq(StringUtils.isNotEmpty(sysDictDataRequest.getHasDefault()),
                        SysDictData::getHasDefault, sysDictDataRequest.getHasDefault())
                //封存（0正常 1停用）
                .eq(StringUtils.isNotEmpty(sysDictDataRequest.getStatus()),
                        SysDictData::getStatus, sysDictDataRequest.getStatus())
                //创建
                .like(StringUtils.isNotEmpty(sysDictDataRequest.getCreateBy()),
                        SysDictData::getStatus, sysDictDataRequest.getCreateBy())
                //修改人
                .like(StringUtils.isNotEmpty(sysDictDataRequest.getModifyBy()),
                        SysDictData::getStatus, sysDictDataRequest.getModifyBy())
                .build();

        return spec;
    }


    @Override
    public TableSupport<SysDictDataResponse> listPage(SysDictDataRequest sysDictDataRequest) {
        Page<SysDictData> page = PageContextHolder.get().toPage();
        page = this.findAllWithPage(page,getParams(sysDictDataRequest));
        return TableSupport.toTableResponse(page, BeanUtils.copyToList(page.getRecords(), SysDictDataResponse.class));
    }


    @Override
    public SysDictDataResponse getDetailById(Long id) {
        SysDictData sysDictData = this.getById(id);
        if (Objects.isNull(sysDictData)){
            throw new BusinessException("查询不存在");
        }
        SysDictDataResponse sysDictDataResponse = new SysDictDataResponse();
        BeanUtils.copyBeanProp(sysDictData, sysDictDataResponse);
        return sysDictDataResponse;
    }


}
